package com.chenglu.android.common.utils;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;



/**
 * @author: lyj
 * @Created: by Copyright 2023/3/7 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：edittext工具类
 */
public class EdittextUtils {

    public static void setClearImage(EditText editText, ImageView ivClear) {
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                //点击时的线
                if (hasFocus) {
//                    mLine.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                    String txt = editText.getText().toString();
                    if (!TextUtils.isEmpty(txt))
//                        点击触碰时的线
                        ivClear.setVisibility(View.VISIBLE);

                } else {
                    ivClear.setVisibility(View.GONE);
                }
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() < 1){
                    ivClear.setVisibility(View.GONE);
                } else{
                    ivClear.setVisibility(View.VISIBLE);
                }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });
    }
}
